package cn.hengzhu.main_manager.web.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.hengzhu.main_manager.repository.dao.ManageMenuMapper;
import cn.hengzhu.main_manager.repository.dao.ManagePermissionMapper;
import cn.hengzhu.main_manager.repository.dao.ManageRolesMapper;
import cn.hengzhu.main_manager.repository.domain.ManageMenu;
import cn.hengzhu.main_manager.repository.domain.ManageMenuExample;
import cn.hengzhu.main_manager.repository.domain.ManagePermission;
import cn.hengzhu.main_manager.repository.domain.ManagePermissionExample;
import cn.hengzhu.main_manager.repository.domain.ManageRoles;
import cn.hengzhu.main_manager.repository.domain.ManageUser;
import cn.hengzhu.main_manager.repository.vo.TreeBaseVO;
import cn.hengzhu.main_manager.repository.vo.TreeOneVO;
import cn.hengzhu.main_manager.repository.vo.TreeThreeVO;
import cn.hengzhu.main_manager.repository.vo.TreeTwoVO;
import cn.hengzhu.main_manager.utils.UserContext;
import io.swagger.annotations.ApiParam;


@Controller
@RequestMapping("/main")
public class PermissionController {
	
	

	@Autowired
	private ManageRolesMapper manageRolesMapper;
	@Autowired
	private ManagePermissionMapper managePermissionMapper;
	@Autowired
	private ManageMenuMapper manageMenuMapper;
	
	
	@ResponseBody
	@RequestMapping(value="/getRoleTree",method= RequestMethod.GET)
	public List<TreeOneVO> getRoleTree(@ApiParam(required = true, name = "roleId", value = "角色Id")Integer roleId) {
		//查询自己的所有权限
		ManageUser user = UserContext.getUser();
		Integer ownRoleId = user.getRoleId();
		ManageRoles permssionByRoleId = manageRolesMapper.selectByPrimaryKey(ownRoleId);
		String ownPermissions = permssionByRoleId.getPermissions();
		List<String> permissionIdList = Arrays.asList(ownPermissions.split(","));
		
	    List<Integer> codesInteger = permissionIdList.stream().map(Integer::parseInt).collect(Collectors.toList());
	    ManagePermissionExample permissionExample=new ManagePermissionExample();
	    permissionExample.createCriteria().andIdIn(codesInteger);
		List<ManagePermission> allPermission = managePermissionMapper.selectByExample(permissionExample);
		
		
		List<Integer> perMissionIds = new ArrayList<>();
		if (roleId != null) {
			// 查询所选角色的所有权限
			ManageRoles manageRoles = manageRolesMapper.selectByPrimaryKey(roleId);
			String permissions = manageRoles.getPermissions();
			List<String> asList = Arrays.asList(permissions.split(","));
			asList.stream().forEach(a -> {
				perMissionIds.add(Integer.valueOf(a));
			});
		}
		TreeOneVO treeOneVO = new TreeOneVO();
		treeOneVO.setNocheck(false);
		List<TreeTwoVO> twoVOList = new ArrayList<>();
		List<Integer> menuIds = allPermission.stream().filter(a -> a.getParentId() == null)
				.collect(Collectors.mapping(ManagePermission::getMenuId, Collectors.toList()));
		ManageMenuExample example=new ManageMenuExample();
		example.createCriteria().andIdIn(menuIds);
		List<ManageMenu> childrenMenuByIds = manageMenuMapper.selectByExample(example);

		List<Integer> parentMenuIds = childrenMenuByIds.stream()
				.collect(Collectors.mapping(ManageMenu::getParentId, Collectors.toList())).stream().distinct()
				.collect(Collectors.toList());
		ManageMenuExample parentExample=new ManageMenuExample();
		example.createCriteria().andIdIn(parentMenuIds);
		List<ManageMenu> parentMenuList = manageMenuMapper.selectByExample(parentExample);
		// 拿到末级节点的父节点的父节点的菜单分类
		Map<Integer, List<ManageMenu>> childrenMenuByParentIdMap = childrenMenuByIds.stream()
				.collect(Collectors.groupingBy(ManageMenu::getParentId, Collectors.toList()));
		Map<Integer, ManageMenu> parentMenuByIdMap = parentMenuList.stream()
				.collect(Collectors.toMap(ManageMenu::getId, a -> a));

		// 末级节点的父节点
		// Map<Integer, ManagePermission> parentPermission =
		// allPermission.stream().filter(a -> a.getParentId() == null)
		// .collect(Collectors.toMap(ManagePermission::getId, a -> a));
		// 筛选出所有的末级节点
		Map<Integer, List<ManagePermission>> childrentPermission = allPermission.stream()
				.filter(a -> a.getParentId() != null).collect(Collectors.groupingBy(ManagePermission::getParentId));
		Map<Integer, ManagePermission> collect = allPermission.stream().filter(a -> a.getParentId() == null).collect(Collectors.toMap(ManagePermission::getMenuId, a -> a));

		parentMenuIds.stream().forEach(a -> {
			TreeTwoVO twoVO = new TreeTwoVO();
			ManageMenu manageMenu = parentMenuByIdMap.get(a);
			twoVO.setName(manageMenu.getName());
			twoVO.setNocheck(false);
			List<ManageMenu> childrentMenuList = childrenMenuByParentIdMap.get(a);
			List<TreeThreeVO> threeListVO = new ArrayList<>();
			childrentMenuList.stream().forEach(b -> {
				TreeThreeVO treeThreeVO = new TreeThreeVO();
				ManagePermission managePermission = collect.get(b.getId());
				if (perMissionIds.contains(managePermission.getId())) {
					treeThreeVO.setChecked(true);
				}
				treeThreeVO.setName(b.getName());
				treeThreeVO.setId(managePermission.getId());
				treeThreeVO.setNocheck(false);
				List<ManagePermission> last = childrentPermission.get(managePermission.getId());
				if (last != null) {
					List<TreeBaseVO> baseList = new ArrayList<>();
					last.stream().forEach(c -> {
						TreeBaseVO baseVO = new TreeBaseVO();
						if (perMissionIds.contains(c.getId())) {
							baseVO.setChecked(true);
						}
						baseVO.setName(c.getName());
						baseVO.setId(c.getId());
						baseVO.setNocheck(false);
						baseList.add(baseVO);
					});
					treeThreeVO.setChildren(baseList);
				}

				threeListVO.add(treeThreeVO);
			});
			twoVO.setChildren(threeListVO);
			twoVOList.add(twoVO);
		});
		treeOneVO.setChildren(twoVOList);
		List<TreeOneVO> list = new ArrayList<>();
		list.add(treeOneVO);
		return list;
	}

}
